layui.use(['form','table','element','laytpl','layer'], function() {
  var table = layui.table;
  var form = layui.form;
  table.render({
    elem: '#supplierTable'
    ,url:'/ssm/consumer/queryList.do'
    ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
    ,page : true
    ,height : "full-125"
    ,cols: [[
      {field:'id', width:80, title: 'ID',type:'numbers'}
      ,{field:'username', title: '账号'}
      ,{field:'password',  title: '密码'}
      ,{field:'real_name', title: '真实姓名'}
      ,{field:'cellphone',  title: '联系电话'}
      ,{field:'email',  title: '邮箱'}
      ,{align:'center', toolbar: '#barTpl', width: 120, title: '操作'}
    ]]
  });

  //工具条点击事件
  	layui.table.on('tool(table)', function(obj){
  		var data = obj.data;
  		var layEvent = obj.event;

  		if(layEvent === 'edit'){ //修改
  			showEditModel(data);
  		} else if(layEvent === 'del'){ //删除
  			doDelete(data);
  		} else if(layEvent == 'detail'){
  			showPermDialog(data.id);
  		}else if(layEvent == 'operateAuth'){
  			perAuthWin(data.id,data.role_name);
  		}
  	});


  	//添加按钮点击事件
  	$("#addBtn").click(function(){
  		showEditModel(null);
  		return false;
  	});

  	$(".search_btn").on("click", function() {
  		var postData={
        		title:'',
        		cellphone:''
        }
  		 var queryValue=$("[name=queryValue]").val().trim();
        var queryKey=$("[name=queryKey]").val().trim();
        postData[queryKey]=queryValue;
         //创建键值对象
        table.reload("supplierTable", {
            page : {
                curr : 1
            },
            where : postData
        })
    });

  	//显示表单弹窗
    function showEditModel(data){
        layer.open({
            type: 1,
            title: data==null?"添加":"修改",
            area: '90%',
            offset: 'auto',
            content: $("#addModel").html(),
            success:function(){
            	form.render('select');
            }
        });
        if(data!=null){
            $("#editForm input[id=id]").val(data.id);
            $("#editForm input[id=username]").val(data.username);
            $("#editForm input[id=password]").val(data.password);
            $("#editForm input[id=real_name]").val(data.real_name);
            $("#editForm input[id=cellphone]").val(data.cellphone);
            $("#editForm input[id=email]").val(data.email);
            form.render('select');
        }
        $("#btnCancel").click(function(){
            layer.closeAll('page');
        });

    }

    	//表单提交事件
    layui.form.on('submit(btnSubmit)', function(data){
            var qUrl="/ssm/consumer/save.do";
            $.post(qUrl,{
                id : $("#id").val(),
                username : $("#username").val(),
                password : $("#password").val(),
                real_name : $("#real_name").val(),
                cellphone : $("#cellphone").val(),
                email : $("#email").val(),
            },function(res){
             if(res.code=="01"){
                 layer.closeAll();
                 layer.msg("保存成功");
                 //刷新父页面
                 layui.table.reload('supplierTable', {});
             }else{
                 layer.msg("保存失败");
             }
         });
            return false;
    });

    
	//删除
	function doDelete(data){
        layer.confirm('确定删除该账号吗？',{icon:3, title:'提示信息'},function(index){
            $.post("/ssm/consumer/del.do",{
            		id : data.id
            },function(data){
                if(data.code=="01"){
                	layer.close(index);
                	layui.table.reload('supplierTable', {});
                    
                }else{
                	layer.close(index);
                    layer.alert("删除失败");
                    
                }

            })
        });
	}

});